﻿#pragma once

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QMainWindow>
#include <QTimer>
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QDir>
#include <QMessageBox>
#include <QWidget>
#include <QApplication>
#include <QHeaderView>
#include <QMenuBar>
#include <QPushButton>
#include <QTableWidget>
#include <QTextEdit>
#include <QStringList>
#include <QFileDialog>
#include <QSettings>
#include "QDesktopServices"
#include "QUrl"
#include "QListView"
#include "QStringListModel"
#include "QDesktopServices"
#include "QStringListModel"
#include <QProcess>
#include <QDockWidget>
#include <QDateTime>
#include <QTcpSocket>
#include <QDialog>
using namespace std;
#include <iostream>
#include <string.h>

#include "camera_qdialog.h"
#include "qt_hread_process.h"
#include "login_dialog.h"
#include "show_qlabel.h"
#include "my_table_widget.h"
#include "commonfunction.h"
#include "devicesizetable.h"

#include "spdlog/spdlog.h"
#include "spdlog/sinks/daily_file_sink.h"
#include "spdlog/fmt/ostr.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"
extern std::shared_ptr<spdlog::logger> logger ;


#include "modbus.h"



extern QImage matToQImage(const Mat mat_in);

#include "recipe_dialog.h"

#include "lightsource.h"

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    MyLightSource LightSource;
    MyLightSource LightSource2;


	

//	QString m_com_port = "COM1";
//	QString m_com_port2 = "COM2";


    int m_camera_number=1;///<成员变量m_camera_number,该视觉系统中，相机的总数量

    //主界面 图像显示
    QWidget* m_image_widget;
    vector< ShowQLabel*> m_vector_show_qlabel;
//    vector< QPictureBox*> m_vector_show_qlabel;
    //检测方法设置界面
    QDialog* m_qdialog_detection = nullptr;
    QTabWidget* m_tabwidget_detection = nullptr;
    vector<QtThreadProcess*>  m_vector_detection_thread;

    //相机设置界面
    QDialog* m_qdialog_camera = nullptr;
    QTabWidget* m_tabwidget_camera = nullptr;
    vector<CameraHKQDialog*> m_vector_camera;

    int m_online=0;///在线连接相机检测时，该值为1，离线检测时，该值为0

    QSettings* m_settings_para = nullptr;
    QString m_para_dir;
    QString m_exe_directory;
    QString m_type_dir = "type";
    QString m_type_name= "";
    QString m_save_data_dir="";
	QString m_save_image_dir="";
	

    // dockwidget pannel
    QDockWidget* m_qdockwidget_pannel= nullptr;
    QPushButton *m_qpushbutton_start_detection  = nullptr;
    QPushButton *m_qpushbutton_stop_detection  = nullptr;
    QPushButton *m_qpushbutton_para  = nullptr;
    QPushButton *m_qpushbutton_camera  = nullptr;
    QPushButton *m_qpushbutton_open_image_dir  = nullptr;
    QPushButton *m_qpushbutton_open_type_dir  = nullptr;
    QPushButton *m_qpushbutton_open_clear  = nullptr;
    QPushButton *m_qpushbutton_exit  = nullptr;

    // dockwidget m_qdockwidget_devicesizetable
    QDockWidget* m_qdockwidget_devicesizetable= nullptr;

    // dockwidget pannel
    QDockWidget* m_qdockwidget_show= nullptr;
    vector<QLabel*> m_vector_qlabel_show_ok;
    vector<QLabel*> m_vector_qlabel_show_all;

     vector<uint64_t>  m_vector_error_number; //错误数量

     vector<uint64_t>  m_vector_all_number;//总数量


    //modibus

    modbus_t *m_modbus_tcp=nullptr;
    QString  m_modbus_ip;
    int      m_modbus_port;
    vector<int> m_plc_trigger;
    vector<int> m_plc_finish;
    vector<int> m_plc_result;
    QTimer*  m_timer_plc  = nullptr;
    QStringList m_save_data_time;


    //登录对话框
    QString m_user_passward;
    QString m_admin_passeard;
    QString m_senior_passeard;
    int m_login_privilege = 0;
    Login_Dialog* m_login_dialog =nullptr;


    //产品切换
    LoadRecipeDialog  m_load_recipe_dialog;

	//存图线程
	SaveImageThread* m_save_image_thread;


public :
    void login();

    void startDetection();

    void stopDetection();

    void clickPara();

    void clickCamera();

    void closeSoftware();

    void returnMainFrame();

    virtual void closeEvent(QCloseEvent* event) override;

    void showResult(VectorOutputResult result);

    void timerLoadImage();

    void timerPLC();

    bool loadPara();

    void initdockDevicesizetable();

    void initdockPannel();

    void initMainWindow();

    void initImageControl();

    void loadRecipe();

    void initdockShow();

	void saveImage(VectorOutputResult result);
};

#pragma execution_character_set("utf-8")
